import Vue from 'vue'
import VueRouter from 'vue-router'
import SchoolList from "@/views/SchoolList";
import SchoolType from "@/views/SchoolType";
import Index from "@/views/index"
import Login from "@/views/Login";
import SchoolHost from "@/views/SchoolHost";
import SchoolLocationType from "@/views/SchoolLocationType";
import test from "@/views/test";
import Error from "@/views/Error";
import AdminList from "@/views/AdminList";
import ExpertList from "@/views/ExpertList";
import SchoolUserList from "@/views/SchoolUserList";
import indexSchoolUser from"@/views/indexSchoolUser"
import indexExpert from"@/views/indexExpert"
import el from "element-ui/src/locale/lang/el";
import EvaluateIndexList from "@/views/EvaluateIndexList";
import EvaluateTaskList from "@/views/EvaluateTaskList";
import EvaluateIndex123 from "@/views/EvaluateIndex123";
import EvaluateTaskListForExpert from "@/views/EvaluateTaskListForExpert";
import EvaluateTaskForSchoolUser from "@/views/EvaluateTaskForSchoolUser";
import EvaluateTaskArrange from "@/views/EvaluateTaskArrange";
import SchoolUserArrange from "@/views/SchoolUserArrange";
import EvaluateIndex123ForSchoolUser from "@/views/EvaluateIndex123ForSchoolUser";
import EvaluateIndex123ForExpert from "@/views/EvaluateIndex123ForExpert";
import EvaluateIndexListForExpert from "@/views/EvaluateIndexListForExpert";
import EvaluateTaskArrangeForExpert from "@/views/EvaluateTaskArrangeForExpert";
import Error2 from "@/views/Error2";
import Error3 from "@/views/Error3";
import Echarts from "@/views/Echarts";
import {Breadcrumb} from "element-ui";
import SchoolUserChangePass from "@/views/SchoolUserChangePass";
import StartEvaluate from "@/views/StartEvaluate";
import EvaluatePage from "@/views/EvaluatePage";
import Welcome from "@/views/Welcome";
import Welcome2 from "@/views/Welcome2";
import Welcome3 from "@/views/Welcome3";
Vue.use(VueRouter)

const routes = [
  {
    path: "/login",
    component: Login,
    //设置show 为false 控制index页面的边栏多余展示
    show:false
  },
  {
    path: "/",
    redirect: '/login'
  },

  {
    path:'/Page01',
    name:'管理员页',
    component:Index,
    redirect:'/welcome3',
    show:true,
    children:[
      {
        path: '/welcome3',
        component: Welcome3
      },
      {
        path:'/SchoolList',
        name:'1',
        component:SchoolList,
        meta:{title:'学校列表'}
      },
      {
        path: '/Echarts',
        component: Echarts
      },
      {
        path: '/EvaluateIndex123',
        component: EvaluateIndex123
      },
      {
        path:'/SchoolType',
        name:'3',
        component:SchoolType,
        meta:{title:'学校类型'}
      },
      {
        path:'/SchoolHost',
        name:'4',
        component:SchoolHost,
        meta:{title:'学校举办者'}
      },
      {
        path:'/SchoolLocationType',
        name:'5',
        component:SchoolLocationType,
        meta:{title:'学校驻地城乡类型'}
      },
      {
        path:'/SchoolUserList',
        component:SchoolUserList,
        meta:{title:'学校账号'}
      },
      {
        path: '/EvaluateTaskList',
        component: EvaluateTaskList,
        meta:{title:'评估任务'}
      },
      {
        path: '/EvaluateTaskArrange',
        component: EvaluateTaskArrange,
        meta:{title:'评估安排'}
      },
      {
        path:'/ExpertList',
        component:ExpertList,
        meta:{title:'专家账号'}
      },
      {
        path: '/Error',
        component: Error
      },
      {
        path: '/AdminList',
        component: AdminList,
        meta:{title:'管理员账号'}
      },
      {
        path: '/EvaluateIndexList',
        component: EvaluateIndexList,
        meta:{title:'评估体系'}
      },

    ]
  },
  {
    path: "/indexSchoolUser",
    component: indexSchoolUser,
    redirect: '/welcome',
    children: [
      {
        path: '/welcome',
        component: Welcome
      },
      {
        path: '/EvaluateIndex123ForSchoolUser',
        component: EvaluateIndex123ForSchoolUser
      },
      {
        path: '/EvaluateTaskForSchoolUser',
        component: EvaluateTaskForSchoolUser,
      },
      {
        path: '/StartEvaluate',
        component: StartEvaluate
      },
      {
        path:"/SchoolUserChangePass",
        component: SchoolUserChangePass
      },
      {
        path: '/SchoolUserArrange',
        name: 'SchoolUserArrange',
        component: SchoolUserArrange,
        meta:{title:'评价任务'}
      },

      {
        path: '/Error3',
        component: Error3
      },

    ]
  },
  {
    path: "/indexExpert",
    component: indexExpert,
    // redirect: '/EvaluateIndexListForExpert',
    redirect: '/welcome2',
    children: [
        {
        path: '/EvaluateTaskListForExpert',
        component: EvaluateTaskListForExpert,
      meta:{title: '评估任务安排',}
        },
      {
        path: '/welcome2',
        component: Welcome2
      },
      {
        path: '/EvaluateIndex123ForExpert',
        component: EvaluateIndex123ForExpert,
      },
      {
        path: '/EvaluateIndexListForExpert',
        component: EvaluateIndexListForExpert,
        meta:{title:'评估体系'}

      },
      {
        path: '/EvaluateTaskArrangeForExpert',
        component: EvaluateTaskArrangeForExpert,
        meta:{title:'评估任务评审'}
      },
      {
        path: '/EvaluatePage',
        component: EvaluatePage,
        meta:{title:'专家评估评审'}
      },
      {
        path: '/Error2',
        component: Error2
      },
      {
        path: '/test',
        component: test
      },
    ]
  }

]

const router = new VueRouter({
  mode: 'history',
  base: process.env.BASE_URL,
  routes
})
// router.beforeEach((to,from,next)=>{
//   if (to.path.startsWith('/login')){
//     next()
//   }
//   else{
//     let admin=JSON.parse(window.localStorage.getItem('access-admin'))
//     if (!admin){
//       next({path:'login'})
//     }
//   else{
//     axios({
//       url:'http://localhost:8081/user/checkToken',
//       method:'get',
//       headers:{
//         token:admin.token
//       }
//     }).then((response)=>{
//       console.log(response.data)
//       if (!response.data){
//         console.log("校验失败");
//         next({path:'/error'})
//       }
//     })
//       next()
//     }
//   }
// })
router.beforeEach((to,from,next)=>{
  if (to.path.startsWith('/login')){
    next()
  }
  else{
    let schooluser=JSON.parse(window.localStorage.getItem('access-schooluser'))
    let expert=JSON.parse(window.localStorage.getItem('access-expert'))
    let admin=JSON.parse(window.localStorage.getItem('access-admin'))
    if (!admin&&!schooluser&&!expert){
      next({path:'/login'})
    }
    else if (admin&&!schooluser&&!expert){
      axios({
        url:'http://localhost:8081/user/checkToken',
        method:'get',
        headers:{
          token:admin.token
        }
      }).then((response)=>{
        console.log(response.data)
        if (!response.data){
          console.log("校验失败");
          next({path:'/error'})
        }
      })
      next()
    }
    else if (schooluser&&!admin&&!expert){
      axios({
        url:'http://localhost:8081/user/checkToken',
        method:'get',
        headers:{
          token:schooluser.token
        }
      }).then((response)=>{
        console.log(response.data)
        if (!response.data){
          console.log("校验失败");
          next({path:'/Error3'})
        }
      })
      next()
    }
    else if (expert&&!schooluser&&!admin){
      axios({
        url:'http://localhost:8081/user/checkToken',
        method:'get',
        headers:{
          token:expert.token
        }
      }).then((response)=>{
        console.log(response.data)
        if (!response.data){
          console.log("校验失败");
          next({path:'/Error2'})
        }
      })
      next()
    }
    else{

    }
  }
})
export default router
